package edu.ts.crowdfunding.product.Controller;

import com.aliyun.oss.OSS;
import io.swagger.v3.oas.annotations.Operation;
import org.junit.jupiter.api.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Tag(value = "文件山上传")
public class UploadController {

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Autowired
    private OSS ossClient;

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "文件上传", description = "文件上传接口")
    public String upload(MultipartFile  file) throws IOException {
        //随机文件名
        String fileName = "tangshan/" + UUID.randomUUID() + "-" + file.getOriginalFilename();
        //上传文件
        ossClient.putObject(bucketName, fileName, file.getInputStream());
        //文件URL
        System.err.println("上传文件路径: " + "https://" + bucketName + "." +endpoint + "/" + fileName);
        return "https://" + bucketName + "." +endpoint + "/" + fileName;
    }
}
